'''
@auther:zhongyingjie
@date:开发时间:2022/11/18 15:55
@file:request_define.PY
@desc:
'''
import requests
import re
class RequestDefine:
    def __init__(self):
        self.session = requests.session()  # 创建对象
    # 创建request请求：请求方式、地址、类型、正文
    def create_request(self,req_type,url,params=None,data_type=None,data=None,headers=None):
        req_type = req_type.upper()
        res =None
        if req_type=='GET': # 请求类型
            res = self.session.get(url=url, params=params, headers=headers)
        elif req_type=='POST':
            if data_type=='json': # 数据类型
                res = self.session.post(url=url, params=params, json=data, headers=headers) # 字典
            else:
                res = self.session.post(url=url, params=params, data=data, headers=headers) # 表格
        return res

    # 封装正则表达式的提取
    def patter_find(self,patter,res): # patter正则表达式、res响应正文
        p = re.compile(patter)  # 编译正则
        result = re.findall(p, res.text)[0]  # findall函数返回的是列表
        return result
    # def close_session(self): # 关闭session对象
    #     self.session.close()

RequestDefine()




